#ifndef __DSTH_BASE_THREAD_H__
#define __DSTH_BASE_THREAD_H__


#include "AcfDelegate.h"
#include "DSTHType.h"
#ifdef IS_WINDOWS
  #define THD_RET unsigned
#else // POSIX
  #include <pthread.h>
  #define THD_RET void*
#endif
#include <string>
using namespace Acf;


namespace dsth {

class BaseThread {
public:
    typedef Delegate<void (const char* name)> SvcFunc;
    BaseThread(const SvcFunc& func, const char* name);
    explicit BaseThread(const char* name);
    BaseThread();

    void SetSvcFunc(const SvcFunc& func) { svc_func_ = func; }
    const char* GetName() const { return name_.c_str(); }
    pid_t GetThreadId() const { return thread_id_; }
    void Run();
    void Join();

    static pid_t GetCurrentThreadId();
private:
    // Can't invoke copy constructor or assignment operator
    BaseThread(const BaseThread&);
    void operator=(const BaseThread&);

    static THD_RET __stdcall ThreadFunc(void* thread);
    SvcFunc svc_func_;
    std::string name_;
    bool running_;
    pid_t thread_id_;
    pthread_t thread_handle_;
};

} // namespace
#endif // __DSTH_BASE_THREAD_H__
